package com.example.word.adapter;

import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.example.dict.R;
import com.example.word.bean.Browse;
import com.example.word.bean.Collect;
import com.example.word.bean.Dict;
import com.example.word.bean.Word;
import com.example.word.util.HttpUtils;
import com.example.word.util.StringUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收藏记录适配器
 */

public class WordManageAdapter extends RecyclerView.Adapter<WordManageAdapter.ViewHolder> {
    private List<Word> list = new ArrayList<>();
    private Context mActivity;
    private ItemListener mItemListener;
    private LinearLayout llEmpty;
    private RecyclerView recyclerView;

    public void setItemListener(ItemListener itemListener) {
        this.mItemListener = itemListener;
    }

    public WordManageAdapter(LinearLayout llEmpty, RecyclerView rvNewsList) {
        this.llEmpty = llEmpty;
        this.recyclerView = rvNewsList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mActivity = viewGroup.getContext();
        View view = LayoutInflater.from(mActivity).inflate(R.layout.item_rv_collect_list, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Word word = list.get(i);
        if (word != null) {
            viewHolder.date.setText(word.getUkphone());
            viewHolder.title.setText(word.getWord());
            if (StringUtils.isNotEmpty(word.getWordZh())) {
                viewHolder.date.setText(word.getWordZh().replace("<split>", ""));
            }

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemListener != null) {
                        mItemListener.ItemClick(word);
                    }
                }
            });
            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);
                    dialog.setIcon(R.drawable.a1);//设置图标
                    dialog.setTitle("球球单词");//设置对话框的标题
                    dialog.setMessage("确认要删除【" + word.getWord() + "】吗? (将清除该单词学习及收藏信息)");
                    dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Map<String, String> requestParam = new HashMap<>();
                            requestParam.put("word", word.getWord());
                            // 调用 API 客户端的方法
                            HttpUtils.sendGetRequest("/deleteWord", requestParam, mActivity, Dict.class, response -> {
                                list.remove(word);

                                notifyDataSetChanged();
                                Toast.makeText(mActivity, "删除成功", Toast.LENGTH_LONG).show();
                                if (list != null && list.size() > 0) {
                                    recyclerView.setVisibility(View.VISIBLE);
                                    llEmpty.setVisibility(View.GONE);
                                } else {
                                    recyclerView.setVisibility(View.GONE);
                                    llEmpty.setVisibility(View.VISIBLE);
                                }
                            });
                        }
                    });
                    dialog.setNeutralButton("取消", (dialog1, which) -> dialog1.dismiss());
                    dialog.show();
                    return false;
                }
            });
        }
    }

    public void addItem(List<Word> listAdd) {
        //如果是加载第一页，需要先清空数据列表
        this.list.clear();
        if (listAdd != null) {
            //添加数据
            this.list.addAll(listAdd);
        }
        //通知RecyclerView进行改变--整体
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView title, date;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            date = itemView.findViewById(R.id.date);
        }
    }

    public interface ItemListener {
        void ItemClick(Word word);
    }
}
